package com.dlwx.parttimetalent.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.LabelEditBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.DialogInter;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.TimeDialog;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditLabelDetailActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.rb_hour)
    RadioButton rbHour;
    @BindView(R.id.rb_day)
    RadioButton rbDay;
    @BindView(R.id.et_time)
    TextView etTime;
    @BindView(R.id.et_server)
    EditText etServer;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.ll_name)
    LinearLayout llName;
    private String mintime = "";
    private String labeltime = "";
    private String labelId = "";
    private String labelType = "";
    private String classId = "";
    private int flag;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            inputshoworhind();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_label_edit);
        ButterKnife.bind(this);
        Intent i = getIntent();
        flag = i.getIntExtra("flag", -1);
        String name = i.getStringExtra("name");
        initView(name);
        switch (flag) {
//             编辑系统子标签
            case Constant.ZERO:
                labelId = i.getStringExtra("id");
                labelType = i.getStringExtra("type");
                getDatas(labelId, labelType);
                break;
//                新增系统子标签
            case Constant.ONE:
                classId = i.getStringExtra("labelId");
                labelId = "";
                break;
//              自定义标签
            case Constant.TWO:
                llName.setVisibility(View.VISIBLE);
                labelId = i.getStringExtra("id");
                labelType = i.getStringExtra("type");
                getDatas(labelId, labelType);
                break;
        }
    }
    private void getDatas(String id, String type) {
        loadingDialog.show();
        OkgoUtil.get(Http.GetEditLabel, EditLabelDetailActivity.this)
                .params("labelid", id)
                .params("labeltype", type)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        LabelEditBean parentBean = DealResult.getInstance().dealDatas(EditLabelDetailActivity.this, response, new TypeToken<LabelEditBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            setDatas(parentBean.getBody());
                        } else {
                            ToastUtils.toastForShort(EditLabelDetailActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(EditLabelDetailActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(LabelEditBean.BodyBean body) {
        etMoney.setText(body.getLabelprice());
        etServer.setText(body.getLabelcontent());
        etName.setText(body.getLabelname());
        if (1 == body.getLabeltime()) {
            labeltime = "1";
            rbDay.setChecked(true);
            rbHour.setChecked(false);
            etTime.setText(body.getMintime() + "天起");
        } else {
            labeltime = "2";
            rbDay.setChecked(false);
            rbHour.setChecked(true);
            etTime.setText(body.getMintime() + "小时起");
        }

    }

    private void initView(String name) {
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText(name);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        mHandler.sendEmptyMessageDelayed(0,500);
        labeltime = "2";
        rbDay.setChecked(false);
        rbHour.setChecked(true);
        etTime.setText("1小时起");
    }

    @OnClick({R.id.title_left_one_btn, R.id.et_money, R.id.rb_hour, R.id.rb_day, R.id.tv_confirm, R.id.et_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.et_money:

                break;
            case R.id.et_time:
                TimeDialog dialog;
                if (rbDay.isChecked()) {
                    dialog = new TimeDialog(this, true, new DialogInter() {
                        @Override
                        public void getInfo(String data) {
                            mintime = data.substring(0, data.length() - 2);
                            etTime.setText(data);
                        }

                        @Override
                        public void getWorkInfo(String start, String end, String unit) {
                        }
                    });

                } else {
                    dialog = new TimeDialog(this, false, new DialogInter() {
                        @Override
                        public void getInfo(String data) {
                            mintime = data.substring(0, data.length() - 3);
                            etTime.setText(data);
                        }

                        @Override
                        public void getWorkInfo(String start, String end, String unit) {
                        }
                    });
                }
                dialog.show();
                break;
            case R.id.rb_hour:
                labeltime = "2";
                rbDay.setChecked(false);
                rbHour.setChecked(true);
                etTime.setText("1小时起");
                break;
            case R.id.rb_day:
                labeltime = "1";
                rbDay.setChecked(true);
                rbHour.setChecked(false);
                etTime.setText("1天起");
                break;
            case R.id.tv_confirm:
                checkInputDatas();
                break;
        }
    }

    private void checkInputDatas() {

        String money = etMoney.getText().toString();
        if (TextUtils.isEmpty(money)) {
            ToastUtils.toastForShort(this, "金额不能为空");
            return;
        }
        String time = etTime.getText().toString();
        if (TextUtils.isEmpty(time)) {
            ToastUtils.toastForShort(this, "时间不能为空");
            return;
        }
        if(time.contains("天")){
            mintime = time.substring(0, time.length() - 2);
        }else if(time.contains("小时")){
            mintime = time.substring(0, time.length() - 3);
        }
        String server = etServer.getText().toString();
//        if (TextUtils.isEmpty(server)) {
//            ToastUtils.toastForShort(this, "服务内容不能为空");
//            return;
//        }
        String name="";
        if(flag==Constant.TWO){
            name = etName.getText().toString();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.toastForShort(this, "标签名称不能为空");
                return;
            }
            confirmCustom(money, server,name);
        }else{
            confirm(money, server,name);
        }


    }

    private void confirm(String money, String server,String name) {
        loadingDialog.show();
        String path="";
          path=Http.EidtSysLabel;

        OkgoUtil.post(path, EditLabelDetailActivity.this)
                .params("labelprice", money)
                .params("labeltime", labeltime)
                .params("mintime", mintime)
                .params("labelcontent", server)
                .params("labelclassid", classId)
                .params("labelid", labelId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(EditLabelDetailActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            EditLabelDetailActivity.this.finish();
                        } else {
                        }
                        ToastUtils.toastForShort(EditLabelDetailActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(EditLabelDetailActivity.this, getString(R.string.bad_net));
                    }
                });
    }
    private void confirmCustom(String money, String server,String name) {
        loadingDialog.show();
        String path="";
         path=Http.EidtCusLabel;
        OkgoUtil.post(path, EditLabelDetailActivity.this)
                .params("labelprice", money)
                .params("labeltime", labeltime)
                .params("mintime", mintime)
                .params("labelcontent", server)
                .params("labelid", labelId)
                .params("labelname", name)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(EditLabelDetailActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            EditLabelDetailActivity.this.finish();
                        } else {
                        }
                        ToastUtils.toastForShort(EditLabelDetailActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(EditLabelDetailActivity.this, getString(R.string.bad_net));
                    }
                });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
//        inputshoworhind();
    }
    public void inputshoworhind(){
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
